import pymysql


class SqlTools(object):
    def __init__(self):
        self.__con = pymysql.connect(user="root",
                                     password="123456",
                                     host="localhost",
                                     database="mydb",
                                     port=3306,
                                     charset="utf8", )
        # 创建游标对象
        self.cur = self.__con.cursor()

    def select_sql(self, sql):
        # 上传
        self.cur.execute(sql)
        self.cur.close()
        self.__con.close()
        return self.cur.fetchall()

    def commit_sql(self, sql):
        self.cur.execute(sql)
        self.__con.commit()
        self.cur.close()
        self.__con.close()

    def input_sql(self, sql):
        if "SELECT" in sql:
            self.select_sql(sql)
        else:
            self.commit_sql(sql)


if __name__ == '__main__':
    SqlTools().commit_sql("INSERT INTO `mydb`.`student`(`sid`, `sname`, `age`, `gender`) "
                          "VALUES ('S_1015', 'yyq', NULL, 'male');")
    print(SqlTools().select_sql("select * from student"))
